﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace TouShuShenQi.BlazorCore.Crawler
{
    public static class CrawlerExtensions
    {
        /// <summary>
        /// 开始爬行回调
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="OnStart"></param>
        /// <returns></returns>
        public static ICrawler CallOnStart(this ICrawler Crawler, EventHandler<OnStartEventArgs> OnStart)
        {
            Crawler.OnStart += OnStart;
            return Crawler;
        }

        /// <summary>
        /// 爬行成功回调
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="OnCompleted"></param>
        /// <returns></returns>
        public static ICrawler CallOnCompleted(this ICrawler Crawler, EventHandler<OnStartEventArgs> OnCompleted)
        {
            Crawler.OnStart += OnCompleted;
            return Crawler;
        }

        /// <summary>
        /// 爬行失败回调
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="OnError"></param>
        /// <returns></returns>
        public static ICrawler CallOnError(this ICrawler Crawler, EventHandler<OnStartEventArgs> OnError)
        {
            Crawler.OnStart += OnError;
            return Crawler;
        }

        /// <summary>
        /// 创建简单蜘蛛
        /// </summary>
        /// <param name="CrawlerUrl">需要爬行的地址</param>
        /// <returns></returns>
        public static ICrawler CreateSimpleCrawler(this string CrawlerUrl)
        {
            return CrawlerUrl.CreateCrawler(new SimpleCrawler(), ProxyAddress: "", Method: "GET", UserAgent: "", Timeout: 5000, CookiesContainer: null);
        }

        /// <summary>
        /// 创建蜘蛛
        /// </summary>
        /// <param name="CrawlerUrl">需要爬行的地址</param>
        /// <param name="Crawler">蜘蛛的类型</param>
        /// <returns></returns>
        public static ICrawler CreateCrawler(this string CrawlerUrl, ICrawler Crawler)
        {
            return CrawlerUrl.CreateCrawler(Crawler, ProxyAddress: "", Method: "GET", UserAgent: "", Timeout: 5000, CookiesContainer: null);
        }

        /// <summary>
        /// 创建蜘蛛
        /// </summary>
        /// <param name="CrawlerUrl">需要爬行的地址</param>
        /// <param name="Crawler">蜘蛛的类型</param>
        /// <param name="ProxyAddress">代理地址:没有就为空</param>
        /// <param name="Method">请求方法</param>
        /// <param name="UserAgent"></param>
        /// <param name="Timeout"></param>
        /// <param name="CookiesContainer"></param>
        /// <returns></returns>
        public static ICrawler CreateCrawler(this string CrawlerUrl,ICrawler Crawler, string ProxyAddress = "", string Method = "GET", string UserAgent = "", int Timeout = 5000, CookieContainer CookiesContainer = null)
        {
            Crawler.CrawlerUrl = new Uri(CrawlerUrl);
            Crawler.ProxyAddress = ProxyAddress;
            Crawler.CookiesContainer = CookiesContainer;
            Crawler.Method = Method;
            Crawler.Timeout = Timeout;

            if (!string.IsNullOrWhiteSpace(UserAgent))
            {
                Crawler.UserAgent = UserAgent;
            }

            return Crawler;
        }


        /// <summary>
        /// 设置请求方式
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="Method"></param>
        /// <returns></returns>
        public static ICrawler SetMethod(this ICrawler Crawler, string Method = "GET")
        {
            Crawler.Method = Method;
            return Crawler;
        }

        /// <summary>
        /// 设置请求URL
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="CrawlerUrl"></param>
        /// <returns></returns>
        public static ICrawler SetUrl(this ICrawler Crawler, string CrawlerUrl)
        {
            Crawler.CrawlerUrl = new Uri(CrawlerUrl);
            return Crawler;
        }

        /// <summary>
        /// 设置Cookie
        /// 为了保持登录状态,第二次设置的时候如果原来已有返回Cookie则不能再次设置
        /// </summary>
        /// <param name="Crawler"></param>
        /// <param name="CookiesContainer"></param>
        /// <returns></returns>
        public static ICrawler SetCookie(this ICrawler Crawler, CookieContainer CookiesContainer = null)
        {
            if(Crawler.CookiesContainer == null && CookiesContainer != null)
            {
                Crawler.CookiesContainer = CookiesContainer;
            }
           
            return Crawler;
        }


        /// <summary>
        /// 开始爬取工作
        /// </summary>
        /// <param name="Crawler"></param>
        /// <returns></returns>
        public static ICrawler ToWorker(this ICrawler Crawler)
        {
            Crawler.Start();
            return Crawler;
        }

        /// <summary>
        /// 开始爬取工作（异步）
        /// </summary>
        /// <param name="Crawler"></param>
        /// <returns></returns>
        public static async Task<ICrawler> ToWorkerAsync(this ICrawler Crawler)
        {
            await Crawler.StartAsync();
            return Crawler;
        }





    }
}
